<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * Cpa接口
 */
class Cpa extends Api
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * cpa登录
     * @ApiMethod   (POST)
     * @param string $channel_code  渠道号
     * @param string $deviceid      设备id
     * @param string $devicetype    设备类型
     * @param string $idfa          idfa
     * @param string $deviceos      设备系统
     * @param string $os_type       系统类型
     * @param string $login_time    登录时间
     * @param string $sign          sign
     */
    public function login()
    {
        $insert['channel_code'] = $this->request->request('channel_code');
        $insert['deviceid']     = $this->request->request('deviceid');
        $insert['devicetype']   = $this->request->request('devicetype');
        $insert['idfa']         = $this->request->request('idfa');
        $insert['deviceos']     = $this->request->request('deviceos');
        $insert['os_type']      = $this->request->request('os_type');
        $insert['login_time']   = $this->request->request('login_time') ?? time();
        $insert['ip']           = request()->ip();
        $insert['is_new']       = Db::name('cpa')->where('deviceid', $insert['deviceid'])->count() ? 0 : 1;

        if (!$insert['channel_code'] || !$insert['deviceid']) {
            $this->error(__('Invalid parameters'));
        }

        if (Db::name('cpa')->insert($insert)) {
            $this->success('success', $insert);
        } else {
            $this->error('fail');
        }
    }
}
